package com.jinsi.controller.tang;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.constant.TangConstant;
import com.jinsi.constant.YltConstant;
import com.jinsi.mapper.tang.ProductMapper;
import com.jinsi.pojo.Commodity;
import com.jinsi.pojo.ProductZtree;
import com.jinsi.service.tang.ProductService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品列表
 * @author 唐文博
 * @Date 2022-08-07
 */

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 展示ztree树接口
     * @return
     */
    @RequestMapping("/getProductZtree")
    public Map<String, Object> getProductZtree() {
        Map<String, Object> map = new HashMap<>();
        map.put("Product", productMapper.getProductZtree());
        return R.ok(map);
    }

    /**
     * ztree树修改接口
     * @param classifyTitle
     * @param classifyId
     * @return
     */
    @RequestMapping("/edit")
    public R editTree(String classifyTitle, int classifyId) {
        try {
            log.info("修改树节点: [开始], 树名称:{},树id：{}",classifyTitle,classifyId);
            productService.updateTreeName(classifyTitle,classifyId);
            log.info("修改树节点: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改树节点: [失败]", e);
            return R.error();
        }
    }

    /**
     * ztree树删除业务接口
     * @param classifyId
     * @return
     */
    @RequestMapping("/del")
    public R delTree(int classifyId) {
        try {
            log.info("删除树节点: [开始], 树id：{}",classifyId);
            productService.delTreeNode(classifyId);
            log.info("删除树节点: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除树节点: [失败]", e);
            return R.error();
        }
    }

    /**
     * ztree树添加业务接口
     * @param productZtree
     * @return
     */
    @RequestMapping("/addTree")
    public R addTree(ProductZtree productZtree) {
        try {
            log.info("添加树节点: [开始], 树信息:{}",productZtree);
            productService.addTreeNode(productZtree);
            log.info("添加树节点: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加树节点: [失败]", e);
            return R.error();
        }
    }


    /**
     * 筛选搜索查询,根据商品名称、商品分类、商品状态、商品标签查询
     * @param page
     * @param limit
     * @param commodityName 根据商品的名称进行搜索查询
     * @param classifyTitle 根据商品的名分类进行搜索查询
     * @param commodityState 根据商品的状态进行搜索查询
     * @param classifyId 商品分类的id
     * @return
     */
    @RequestMapping("/getCommoditySee")
    public R getCommoditySee(Integer page, Integer limit, String commodityName,
                                 String classifyTitle, Integer commodityState,
                             Integer classifyId){
        PageHelper.startPage(page,limit);
        List<Commodity> list = productService.getCommoditySee(commodityName,classifyTitle,
                commodityState, TangConstant.COMMODITY_ISDEL,classifyId);
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);
        return R.custom(YltConstant.LAYUI_CODE,
                YltConstant.LAYUI_MSG,
                pageInfo.getList(),
                pageInfo.getTotal());
    }

    /**
     * 点击上架 修改上架状态‘
     * @param commodityId
     * @param commodityState
     * @return
     */
    @RequestMapping("/updateCommodity")
    public R updateCommodity(Integer commodityId, Integer commodityState) {
        try {
            log.info("修改商品状态上架和下架: [开始], 商品id:{},商品状态码：{}",commodityId,commodityState);
            productService.updateCommodity(commodityId,commodityState);
            log.info("修改商品状态上架和下架: [结束] 结果：{}","成功");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除商品日志: [失败]", e);
            return R.error();
        }
    }
}
